//
//  IWSearchView.m
//  SINA
//
//  Created by 王相超 on 16/3/28.
//  Copyright © 2016年 chuanzhi. All rights reserved.
//

#import "IWSearchView.h"

@interface IWSearchView ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tf_rightCon;

@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;

@end

@implementation IWSearchView

+(instancetype)searchView{
    
    return [[[NSBundle mainBundle] loadNibNamed:@"IWSearch" owner:nil options:nil]lastObject];
    
}



-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        
    }
    
    return self;
}


-(void)awakeFromNib{
    
    UIImageView *leftView = [[UIImageView alloc] init];
    
    leftView.image = [UIImage imageNamed:@"searchbar_textfield_search_icon"];
    
    leftView.contentMode = UIViewContentModeCenter;
    
    leftView.width = self.textField.height;
    leftView.height = self.textField.height;
    
    self.textField.leftView = leftView;
    
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    
    self.textField.delegate = self;
    
    
}

-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        
        
    }
    
    return self;
}

- (IBAction)cancelBtn:(UIButton *)sender {
    
    [self.textField resignFirstResponder];
    self.tf_rightCon.constant = 0;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self layoutIfNeeded];
    }];
}


-(void)textFieldDidBeginEditing:(UITextField *)textField{
    
    
    self.tf_rightCon.constant = self.cancelBtn.width;
    
    [UIView animateWithDuration:0.25 animations:^{
        
        [self layoutIfNeeded];
    }];
    
    
}












@end
